//
//  ZCEnrollView.m
//  zctx
//
//  Created by apple on 16/2/23.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCEnrollView.h"

#define ItemWidth 55
#define ItemHeight 55
#define MaxItemCount 3

@interface ZCEnrollView ()<UITextFieldDelegate>
@property (nonatomic,strong) NSMutableArray *itemArray;
@property (nonatomic,strong)NSMutableArray *photoMenuItems;
@property (nonatomic,strong)UIButton *selectBut;
@property (nonatomic,strong)NSMutableArray *ButtonArray;



@end

@implementation ZCEnrollView
-(id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.ButtonArray = [[NSMutableArray alloc] init];
        UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:frame];
        [self addSubview:bgImageView];
        [self addLabelTitle];
        [self setup];
        [self OtherInfoLable];
        [self addSelectButton];
        [self addSelectButton];

    }
    return self;
}

#pragma mark 上传头像
- (void)setup{
    _photoScrollView = [[UIView alloc]init];
    [self addSubview:_photoScrollView];
    if (IS_IPHONE_4_OR_LESS ||IS_IPHONE_5) {
        [_photoScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.labelTitle.top).with.offset(-5);
            make.height.mas_equalTo(@(63));
            make.left.equalTo(self.labelTitle.right).with.offset(0);
            make.right.equalTo(self.right).with.offset(0);
        }];
    }else{
        [_photoScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.labelTitle.top).with.offset(-5);
            make.height.mas_equalTo(@(63));
            make.left.equalTo(self.labelTitle.right).with.offset(0);
            make.right.equalTo(self.right).with.offset(0);
        }];
    }
    _photoMenuItems = [[NSMutableArray alloc]init];
    _itemArray = [[NSMutableArray alloc]init];
    [self initlizerScrollView:nil];
}

-(void)messagePhotoItemView:(MessagePhotoMenuItem *)messagePhotoItemView didSelectDeleteButtonAtIndex:(NSInteger)index{
    [self.PicArray removeObjectAtIndex:index];
    [self initlizerScrollView:self.PicArray];
}

-(void)initlizerScrollView:(NSArray *)imgList{
    [self.photoScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.PicArray = [imgList mutableCopy];
    CGFloat  itmWidth;
    if (IS_IPHONE_4_OR_LESS ||IS_IPHONE_5) {
        itmWidth = 45;
    }else{
        itmWidth = 55;
    }
    for(int i=0;i<imgList.count;i++){
        UIImage *image = [imgList objectAtIndex:i];
        MessagePhotoMenuItem *photoItem = [[MessagePhotoMenuItem alloc]initWithFrame:CGRectMake(i * (itmWidth+11),0, itmWidth, 63)];
        photoItem.delegate = self;
        photoItem.tag = i;
        photoItem.contentImage = image;
        [self.photoScrollView addSubview:photoItem];
        [self.itemArray addObject:photoItem];
    }
    if(imgList.count<MaxItemCount){
        UIButton *btnphoto=[UIButton buttonWithType:UIButtonTypeCustom];
        [btnphoto setFrame:CGRectMake( (itmWidth+11) * imgList.count,10, itmWidth, itmWidth)];//
        if (imgList.count==0) {
            [btnphoto setImage:[UIImage imageNamed:@"raise_pic"] forState:UIControlStateNormal];
        }else{
            [btnphoto setImage:[UIImage imageNamed:@"raise_add"] forState:UIControlStateNormal];
        }
        btnphoto.tag = imgList.count;
        //给添加按钮加点击事件
        [btnphoto addTarget:self action:@selector(openMenu:) forControlEvents:UIControlEventTouchUpInside];
        [self.photoScrollView addSubview:btnphoto];
    }
}

-(void)openMenu:(UIButton*)sender{
    //在这里呼出下方菜单按钮项
    if (_delegate && [_delegate respondsToSelector:@selector(didSelecteZCEnrollViewPhotoMenuItem:atIndex:)] ) {
        NSInteger index = sender.tag;
        [self.delegate didSelecteZCEnrollViewPhotoMenuItem:self.PicArray atIndex:index];
    }
}

#pragma mark 学校、年级、班级、姓名、宣言
-(void)OtherInfoLable{
    NSArray *titleArray = [NSArray arrayWithObjects:@"学校:",@"年级:",@"班级:",@"姓名:",@"宣言:",nil];
    for(int i=0;i<5;i++){
        UILabel *titleLable = [[UILabel alloc]initWithFrame:CGRectMake(20,i * (25+12)+80,45,25)];
        titleLable.text = titleArray[i];
        titleLable.font = [UIFont systemFontOfSize:15];
        [self addSubview:titleLable];
    }
    for (int i=0; i<4; i++) {
        UILabel *contentLable = [[UILabel alloc]initWithFrame:CGRectMake(90,i * (25+12)+80,SCREEN_WIDTH - 90 - 50,25)];
        contentLable.text = titleArray[i];
        contentLable.layer.borderWidth = 1;
        contentLable.layer.borderColor = RGB(111, 202, 224).CGColor;
        contentLable.font = [UIFont systemFontOfSize:15];
        [self addSubview:contentLable];
    }
}

#pragma mark 选择宣言
-(void)addSelectButton{
    for (int i=0; i<3; i++) {
        UIButton *selectButton = [[UIButton alloc]initWithFrame:CGRectMake(50,(i+5) * (25+12)+80,25, 25)];
        selectButton.tag = i+10;
        [selectButton setBackgroundImage:[UIImage imageNamed:@"Pay_select_n"] forState:UIControlStateNormal];
        [selectButton setBackgroundImage:[UIImage imageNamed:@"Pay_select_p"] forState:UIControlStateSelected];
        if (i==0) {
            selectButton.selected = YES;
            self.selectBut = selectButton;
        }
        [self.ButtonArray addObject:selectButton];
        [self addSubview:selectButton];
        [selectButton addTarget:self action:@selector(ClickSelectButtonIndex:) forControlEvents:UIControlEventTouchUpInside];
        
        UITextField * selectTextfield = [[UITextField alloc]initWithFrame:CGRectMake(90, (i+5) * (25+12)+80, SCREEN_WIDTH - 90 - 50, 25)];
        selectTextfield.tag = i +20;
        selectTextfield.delegate = self;
        selectTextfield.textAlignment = NSTextAlignmentCenter;
        selectTextfield.layer.borderWidth = 1;
        selectTextfield.layer.cornerRadius = 5;
        selectTextfield.layer.borderColor = RGB(111, 202, 224).CGColor;
        [self addSubview:selectTextfield];
    }
    
    UIButton *firmButton = [[UIButton alloc]initWithFrame:CGRectMake(60,8.5 * (25+12)+80,SCREEN_WIDTH-120, 40)];
    firmButton.backgroundColor = RGB(253, 189, 74);
    firmButton.layer.cornerRadius = 20;
    firmButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [firmButton setTitle:@"确认报名" forState:UIControlStateNormal];
    [self addSubview:firmButton];
    [firmButton addTarget:self action:@selector(ClickFirmButton) forControlEvents:UIControlEventTouchUpInside];
}

-(void)ClickSelectButtonIndex:(UIButton *)sender{
    NSLog(@"sender.tag = %ld",sender.tag);
    for (UIButton *btton in self.ButtonArray) {
        btton.selected = NO;
    }
    sender.selected = YES;
//    if(sender!=self.selectBut){
//        self.selectBut.selected=NO;
//        self.selectBut=sender;
//    }
//    self.selectBut.selected=YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    for (UIButton *btton in self.ButtonArray) {
        btton.selected = NO;
    }
    if (textField.tag == 20) {
        UIButton *button =(UIButton *)self.ButtonArray[0];
        button.selected = YES;
    }else if (textField.tag ==21){
        UIButton *button =(UIButton *)self.ButtonArray[1];
        button.selected = YES;
    }else{
        UIButton *button =(UIButton *)self.ButtonArray[2];
        button.selected = YES;
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
#pragma mark 点击确认按钮
-(void)ClickFirmButton{
    NSLog(@"确认");

}

- (void)addLabelTitle {
    [self addSubview:self.labelTitle];
    [self.labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.top).with.offset(11);
        make.height.mas_equalTo(@(21));
        make.width.mas_equalTo(@(70));
        make.left.mas_equalTo(@(20));
    }];
}

- (UILabel *)labelTitle {
    if (!_labelTitle) {
        _labelTitle = [[UILabel alloc] init];
        [_labelTitle setTextColor:[UIColor colorWithHexString:@"#404040"]];
        _labelTitle.font = [UIFont systemFontOfSize:14];
        _labelTitle.text = @"上传照片: ";
    }
    return _labelTitle;
}



@end
